#!/bin/sh

# Slackware build script for bsnes

# Copyright 2019-2020 Hunter Sezen California, USA
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

PRGNAM=bsnes
LIBNAM=${PRGNAM}_libretro
VERSION=${VERSION:-114}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

LIBDIRSUFFIX=
[ "$ARCH" = x86_64 ] && LIBDIRSUFFIX=64

set -eu

if pkg-config --exists sdl2 && pkg-config --exists gtksourceview-2.0; then
  STANDALONE="${STANDALONE:-yes}"
else
  STANDALONE=no
fi

if [ "${LIBRETRO:=yes}" != yes ] && [ "$STANDALONE" != yes ]; then
  printf %s\\n \
    'ERROR: Both the standalone and Libretro builds are disabled.' >&2
  exit 1
fi

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf ${PRGNAM}_v$VERSION-source
unzip $CWD/${PRGNAM}_v$VERSION-source.zip
cd ${PRGNAM}_v$VERSION-source
chown -R root:root .
find -L . \
 \( -perm 777 -o -perm 775 -o -perm 750 -o -perm 711 -o -perm 555 \
  -o -perm 511 \) -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 640 -o -perm 600 -o -perm 444 \
  -o -perm 440 -o -perm 400 \) -exec chmod 644 {} \;

if ! pkg-config --exists libpulse || [ "${PULSE:-yes}" != yes ]; then
  sed -i 's/ audio.pulseaudio audio.pulseaudiosimple//' ruby/GNUmakefile
fi

if ! pkg-config --exists openal || [ "${OPENAL:-yes}" != yes ]; then
  sed -i 's/ audio.openal//' ruby/GNUmakefile
fi

(
  cd $PRGNAM

  if [ "$STANDALONE" = yes ]; then
    make \
      target=$PRGNAM \
      compiler=clang++

    # The install target in bsnes/target-bsnes/GNUmakefile intentionally
    # does not support installing as root or DESTDIR. It is easier to
    # handle the install here instead.

    mkdir -p "$PKG/usr/games" \
             "$PKG/usr/share/applications" \
             "$PKG/usr/share/icons" \
             "$PKG/usr/share/$PRGNAM/Database" \
             "$PKG/usr/share/$PRGNAM/Locale"

    cp out/$PRGNAM "$PKG/usr/games"
    cp target-$PRGNAM/resource/$PRGNAM.desktop "$PKG/usr/share/applications"
    cp target-$PRGNAM/resource/$PRGNAM.png "$PKG/usr/share/icons"
    cp Database/* "$PKG/usr/share/$PRGNAM/Database"
    cp Locale/* "$PKG/usr/share/$PRGNAM/Locale"
  fi

  if [ "$LIBRETRO" = yes ]; then
    make clean
    make \
      target=libretro \
      compiler=clang++

    install -Dm0644 out/$LIBNAM.so \
      $PKG/usr/lib${LIBDIRSUFFIX}/libretro/$LIBNAM.so

    # bsnes_libretro.info from:
    # https://github.com/libretro/libretro-super/blob/5d35cf946b352fe03cff293612a17d5d6efa8410/dist/info/bsnes_libretro.info
    install -Dm0644 $CWD/$LIBNAM-info \
      $PKG/usr/lib${LIBDIRSUFFIX}/libretro/info/$LIBNAM.info
  fi
)

find $PKG -print0 | xargs -0 file | grep -e "executable" -e "shared object" |
  grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null || true

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp GPLv3.txt LICENSE.txt README.md $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.${PKGTYPE:-tgz}
